package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;

import com.ztesoft.bss.common.util.SpringUtil;

/**
 * @author lyx
 * @version V1.0
 * @Description:
 * @date 2021/3/17
 * @Package com.iwhalecloud.bss.kite.cucc.service.attr.handler
 */
public class ServiceStartDateAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(data.getSceneInstId());
        String askSource = "";
        if (!KiteObjectUtils.isEmpty(sceneInst)) {
            askSource = sceneInst.getAskSource();
        }
        //模版还原时，不做校验,起租等环节不校验,因为好多单子被这个校验住了,页面都进不去了
        String extParam = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "entry");
        if ("sceneTempl".equals(data.getExtAttributes().get("entry")) || "sceneTempl".equals(extParam)
            || KiteStringUtils.isEqual(LocalKeyConsts.ASK_SOURCE_ORDER, askSource)) {
            return;
        }
        //变更业务的场景初始化过程中不校验
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))) {
            return;
        }
        // 追加订购
        if (KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.ADD_ORDE))) {
            return;
        }
        int startDate = Integer.parseInt(value.replace("-", ""));
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        int nowdate = Integer.parseInt(df.format(calendar.getTime()));
        if (startDate <= nowdate) {
            StringBuffer sb = new StringBuffer();
            sb.append(attr.getCname()).append("需要大于当前时间，请重新输入！");
            ErrorConsts.BUSI_COMMON_ERROR.throwOut(sb.toString());
        }
    }
}
